package com.sicau.socialpracticesystem.controller;


import com.sicau.socialpracticesystem.common.constant.UrlConstants;
import com.sicau.socialpracticesystem.common.myenum.TeamMemberStateEnum;
import com.sicau.socialpracticesystem.common.returning.ResultData;
import com.sicau.socialpracticesystem.config.aop.ValidateAspect;
import com.sicau.socialpracticesystem.entity.param.PageParam;
import com.sicau.socialpracticesystem.entity.param.teammember.TeamApplyParam;
import com.sicau.socialpracticesystem.entity.param.teammember.TeamApplyQueryParam;
import com.sicau.socialpracticesystem.entity.param.teammember.TeamApplyStateParam;
import com.sicau.socialpracticesystem.entity.po.Team;
import com.sicau.socialpracticesystem.entity.po.TeamMember;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.teammember.TeamMemberVO;
import com.sicau.socialpracticesystem.service.TeamMemberService;
import com.sicau.socialpracticesystem.service.TeamService;
import com.sicau.socialpracticesystem.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 团队成员信息表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@PreAuthorize("isAuthenticated()")
@Api(tags = "团队成员信息相关接口")
@RequestMapping(UrlConstants.TEAM_MEMBER_PREFIX)
public class TeamMemberController {

    @Resource
    private TeamMemberService teamMemberService;

    @Resource
    private TeamService teamService;


    @GetMapping
    @ApiOperation("根据团队id获取团队对应团队的成员情况")
    @ApiImplicitParam(name = "teamId", value = "团队id")
    public ResultData<PageDataVO<TeamMemberVO>> getTeamsMembers(Integer teamId, PageParam pageParam) {
        return new ResultData<PageDataVO<TeamMemberVO>>().OK()
                .setObj(teamMemberService.queryTeamMemberByTeamId(teamId, pageParam));
    }

    @PutMapping("{id}")
    @ApiOperation("修改团队成员申请状态")
    public ResultData<Object> updateTeamApplyState(@PathVariable Integer id, @RequestBody TeamApplyStateParam stateParam) {
        TeamMember teamMember = teamMemberService.getById(id);
        Team team = teamService.getById(teamMember.getTeamId());
        if (!team.getCreatorId().equals(UserContext.get().getId())) {
            return new ResultData<>().FAILED().setResultIns("只有对应团队的队长审核自己团队的成员申请情况");
        }
        TeamMemberStateEnum teamMemberStateEnum = TeamMemberStateEnum.getEnum(stateParam.getState());
        if (teamMemberStateEnum != null) {
            teamMember.setState(stateParam.getState());
            return teamMemberService.updateById(teamMember)
                    ? new ResultData<>().OK()
                    : new ResultData<>().FAILED();
        } else {
            return new ResultData<>().FAILED().setResultIns("传入的审核状态不正确");
        }
    }

    @GetMapping("/apply")
    @ApiOperation("获取团队成员申请信息（只有队长调用有结果）")
    public ResultData<PageDataVO<TeamMemberVO>> getTeamsApplyMembers(TeamApplyQueryParam teamApplyQueryParam) {
        return new ResultData<PageDataVO<TeamMemberVO>>().OK()
                .setObj(teamMemberService.getTeamsApplyMembers(teamApplyQueryParam));
    }

    @PostMapping("/apply")
    @ApiOperation("当前用户申请加入团队")
    @ValidateAspect.ParamValidate
    public ResultData apply(@RequestBody @Valid TeamApplyParam teamApplyParam, BindingResult bindingResult){
        teamMemberService.applyTeam(teamApplyParam);
        return new ResultData().OK();
    }


}

